package cn.yuxiaoyuan.feign;

import cn.yuxiaoyuan.pojo.BaseData;
import cn.yuxiaoyuan.pojo.DistanceDto;
import cn.yuxiaoyuan.pojo.domain.ChargeRuleReturn;
import cn.yuxiaoyuan.pojo.domain.ChargeRuleStart;
import cn.yuxiaoyuan.pojo.domain.ChargeRuleWait;
import cn.yuxiaoyuan.result.JSONResult;
import cn.yuxiaoyuan.service.IChargeRuleReturnService;
import cn.yuxiaoyuan.service.IChargeRuleRewardService;
import cn.yuxiaoyuan.service.IChargeRuleStartService;
import cn.yuxiaoyuan.service.IChargeRuleWaitService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author solargen
 * @version 1.0
 * @date 2025/6/28 15:28
 */
@RestController
@Validated
@RequestMapping("/manager/calculate")
public class RuleFeignApiImpl implements RuleFeignApi{
    @Autowired
    private IChargeRuleReturnService chargeRuleReturnService; //返程
    @Autowired
    private IChargeRuleStartService chargeRuleStartService;//起步价
    @Autowired
    private IChargeRuleWaitService chargeRuleWaitService;

    @Override
    public JSONResult price(DistanceDto distanceDto) {
        //三张数据表进行计算  得到预计金额
        BigDecimal totalPrice = new BigDecimal(0);

        //计算返程费
        ChargeRuleReturn ruleReturn = chargeRuleReturnService.getById(1L);
        //判断 预计里程数 有没有超过免费返程数
        Float distanceKm = distanceDto.getDistanceKm();
        int round = Math.round(distanceKm);//四舍五入
        int returnMileage = ruleReturn.getFreeBaseReturnMileage().intValue();
        if(round > returnMileage){
            //计算超出的钱
            BigDecimal multiply = ruleReturn.getExceedEveryKmAmount().multiply(new BigDecimal(round - returnMileage));
            totalPrice = totalPrice.add(multiply);
        }

        //起步价
        int hours = distanceDto.getCreateTime().getHours();
        ChargeRuleStart ruleStart = chargeRuleStartService.getOne(new LambdaQueryWrapper<ChargeRuleStart>()
                .gt(ChargeRuleStart::getHourStart, hours)
                .or()
                .lt(ChargeRuleStart::getHourEnd, hours)
        );
        //判断有没有超过基础里程
        if (round > ruleStart.getBaseMileage().intValue()){
            //起步价 +  超出里程钱
            BigDecimal overMileageMoney = ruleStart.getExceedEveryKmAmount()
                    .multiply(new BigDecimal(round - ruleStart.getBaseMileage().intValue()));
            totalPrice = totalPrice.add(overMileageMoney);
        }
        //只加起步价
        totalPrice = totalPrice.add(ruleStart.getAmount());
        return JSONResult.success(totalPrice);
    }

    @Override
    public JSONResult getBaseRule() {
        BaseData baseData = new BaseData();
        int hours = new Date().getHours();
        ChargeRuleStart ruleStart = chargeRuleStartService.getOne(new LambdaQueryWrapper<ChargeRuleStart>()
                .le(ChargeRuleStart::getHourStart, hours)
                .ge(ChargeRuleStart::getHourEnd, hours)
        );
        baseData.setBaseMileage(ruleStart.getBaseMileage());
        baseData.setAmount(ruleStart.getAmount());
        baseData.setExceedEveryKmAmount(ruleStart.getExceedEveryKmAmount());


        ChargeRuleWait wait = chargeRuleWaitService.getById(1L);
        baseData.setFreeBaseWaitingMinute(wait.getFreeBaseWaitingMinute());
        baseData.setExceedEveryMinuteAmount(wait.getExceedEveryMinuteAmount());

        ChargeRuleReturn ruleReturn = chargeRuleReturnService.getById(1L);
        baseData.setFreeBaseReturnMileage(ruleReturn.getFreeBaseReturnMileage());
        baseData.setExceedEveryKmReturnAmount(ruleReturn.getExceedEveryKmAmount());
        return JSONResult.success(JSON.toJSONString(baseData));
    }

}
